﻿using System;
using System.Dynamic;
using BalanceManager.Helpers;
using BalanceManager.Model;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using BalanceManager.Services;

namespace BalanceManager.ViewModels
{
    public class HistoryViewModel : BaseViewModel, INavigable
    {
        private ObservableCollection<HistoryItem> _items;
        private TreeNode<HistoryItem> _itemsTree;
        private readonly HistoryProvider _historyProvider;

        public HistoryViewModel(IDataService dataService)
        {
            if (dataService == null)
                throw new ArgumentException("dataService");

            _historyProvider = new HistoryProvider(dataService);
            _itemsTree = _historyProvider.HistoryTree(DateTime.Now, Period.Today);
        }

        public ObservableCollection<TreeNode<HistoryItem>> ItemsTree
        {
            get { return _itemsTree.Children; }
        }

        Period _period;
        public Period Period
        {
            get
            {
                return _period;
            }
            set 
            {
                _period = value;
                _itemsTree = _historyProvider.HistoryTree(DateTime.Now, _period);
                OnPropertyChanged("TotalExpensesAmount");
                OnPropertyChanged("TotalProfitsAmount");
                OnPropertyChanged("ItemsTree");
                
            }
        }

        public int TotalExpensesAmount
        {
            get
            {
                var amount = Convert.ToInt32(_historyProvider.TotalExpensesAmount);
                if (amount == 0) return 1;
                return amount;
            }
        }

        public int TotalProfitsAmount
        {
            get
            {
                var amount = Convert.ToInt32(_historyProvider.TotalProfitsAmount);
                if (amount == 0) return 1;
                return amount;
            }
        }

        public void Activate(IDictionary<string, object> parameters) { }

        public void Deactivate(IDictionary<string, object> parameters) { 
        }
    }
}
